//
//  YMProblemViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/26.
//

#import "YMProblemViewController.h"
#import "YMProblemMainListCell.h"
#import "YMMainDefaultEmptyCell.h"
#import "YMMineService.h"


@interface YMProblemViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tvMain;

@property (nonatomic, strong) NSMutableArray *arrList;

@property (nonatomic, assign) NSInteger indexChoose;

@property (nonatomic, assign) NSInteger pageNo;


@end

@implementation YMProblemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _indexChoose = -1;
    [self setUI];
    [self loadData:YES];
}

- (void)setUI{
    [self showNavTitle:KLanguage(@"questions_txt")];
    [self.view addSubview:self.tvMain];
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
    WEAK_SELF;
    [SThemeUtil refershHead:self.tvMain block:^{
        [weakSelf loadData:YES];
    }];
    [SThemeUtil refershFooter:self.tvMain block:^{
        [weakSelf loadData:NO];
    }];
}

- (void)loadData:(BOOL)isRefresh{
    if (isRefresh){
        _pageNo = 1;
    }
    WEAK_SELF;
    [YMMineService requestMineQuestionListDataPageNo:_pageNo success:^(NSArray * _Nonnull arrData) {
        [weakSelf.tvMain.mj_header endRefreshing];
        if (isRefresh){
            [weakSelf.arrList removeAllObjects];
            [weakSelf.tvMain.mj_footer endRefreshing];
        }else{
            if (arrData.count > 0){
                [weakSelf.tvMain.mj_footer endRefreshing];
            }else{
                [weakSelf.tvMain.mj_footer endRefreshingWithNoMoreData];
            }
        }
        if (arrData.count > 0){
            [weakSelf.arrList addObjectsFromArray:arrData];
            weakSelf.pageNo++;
        }
        [weakSelf.tvMain reloadData];
    } failure:^(NSString * _Nonnull message) {
        [weakSelf.tvMain.mj_header endRefreshing];
        [weakSelf.tvMain.mj_footer endRefreshing];
        [SHUD showInfo:message];
    }];
}

#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.arrList.count > 0){
        return self.arrList.count;
    }else{
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrList.count > 0){
        YMProblemMainListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMProblemMainListCell" forIndexPath:indexPath];
        cell.isChoose = indexPath.section == _indexChoose;
        if (indexPath.section < self.arrList.count){
            cell.dict = [SDataUtil dictOf:self.arrList[indexPath.section]];
        }
        return cell;
    }else{
        YMMainDefaultEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMMainDefaultEmptyCell" forIndexPath:indexPath];
        cell.strDes = KLanguage(@"status_empty");
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.arrList.count > 0){
        if (indexPath.section == _indexChoose){
            WEAK_SELF;
            return [tableView fd_heightForCellWithIdentifier:@"YMProblemMainListCell" configuration:^(YMProblemMainListCell *cell) {
                cell.isChoose = YES;
                if (indexPath.section < weakSelf.arrList.count){
                    cell.dict = [SDataUtil dictOf:weakSelf.arrList[indexPath.section]];
                }
            }];
        }else{
            return [YMProblemMainListCell getCellHeight];
        }
    }else{
        return [YMMainDefaultEmptyCell getCellHeight];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return [SFitUtil fit:15];
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return [SFitUtil fit:15];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrList.count > indexPath.section){
        if (_indexChoose == indexPath.section){
            _indexChoose = -1;
        }else{
            _indexChoose = indexPath.section;
        }
        [self.tvMain reloadData];
    }
}

#pragma mark -- setter and getter
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = NO;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_tvMain registerClass:[YMProblemMainListCell class] forCellReuseIdentifier:@"YMProblemMainListCell"];
        [_tvMain registerClass:[YMMainDefaultEmptyCell class] forCellReuseIdentifier:@"YMMainDefaultEmptyCell"];
    }
    return _tvMain;
}
- (NSMutableArray *)arrList{
    if (_arrList == nil){
        _arrList = [[NSMutableArray alloc] init];
    }
    return _arrList;
}


@end
